﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormConnSetup.cs" company="">
//   
// </copyright>
// <summary>
//   The form conn setup.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace NetworkSupervisor
{
    using System;
    using System.Windows.Forms;

    using DevExpress.Xpo;
    using DevExpress.Xpo.DB;
    using DevExpress.XtraEditors;

    using NetworkSupervisor.Helper;

    /// <summary>The form conn setup.</summary>
    public partial class FormConnSetup : DevExpress.XtraEditors.XtraForm
    {
        #region Fields

        /// <summary>The info.</summary>
        private ConnectionInfo info;

        #endregion

        #region Constructors and Destructors

        /// <summary>Initializes a new instance of the <see cref="FormConnSetup"/> class.</summary>
        public FormConnSetup()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Methods

        /// <summary>The form conn setup_ load.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void FormConnSetup_Load(object sender, EventArgs e)
        {
            this.Icon = Properties.Resources.app;
            this.info = new ConnectionInfo();
        }

        /// <summary>The form conn setup_ shown.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void FormConnSetup_Shown(object sender, EventArgs e)
        {
            this.cbType.Text = this.info.Type;
            this.txtDatabase.Text = this.info.Database;
            this.txtPassword.Text = this.info.Password;
            this.txtServer.Text = this.info.Server;
            this.txtUserid.Text = this.info.Userid;

            if (this.info.Type == "mssql")
            {
                this.txtPassword.Enabled = this.txtServer.Enabled = this.txtUserid.Enabled = true;
                if (this.txtServer.Text == ".")
                {
                    this.txtUserid.Enabled = this.txtPassword.Enabled = false;
                }
            }

            if (this.info.Type == "access")
            {
                this.txtPassword.Enabled = this.txtServer.Enabled = this.txtUserid.Enabled = false;
            }
        }

        /// <summary>The btn cancel_ click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>The btn save_ click.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            this.info.Type = this.cbType.Text;
            this.info.Database = this.txtDatabase.Text;
            this.info.Password = this.txtPassword.Text;
            this.info.Server = this.txtServer.Text;
            this.info.Userid = this.txtUserid.Text;
            this.info.UpdateConnectionString();
            try
            {
                XpoDefault.DataLayer = XpoDefault.GetDataLayer(this.info.ConnectionString, AutoCreateOption.DatabaseAndSchema);
                XpoDefault.Session = null;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.InnerException.Message);
                return;
            }

            XtraMessageBox.Show("保存成功");
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>The cb type_ selected index changed.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = this.cbType.Text;
            if (type == "mssql")
            {
                this.txtPassword.Enabled = this.txtServer.Enabled = this.txtUserid.Enabled = true;
                this.txtDatabase.Text = "NetworkSupervisor";
            }

            if (type == "access")
            {
                this.txtPassword.Enabled = this.txtServer.Enabled = this.txtUserid.Enabled = false;
                this.txtDatabase.Text = "data.db";
            }
        }

        /// <summary>The txt server_ key up.</summary>
        /// <param name="sender">The sender. </param>
        /// <param name="e">The e. </param>
        private void txtServer_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.txtServer.Text == ".")
            {
                this.txtUserid.Enabled = this.txtPassword.Enabled = false;
            }
            else
            {
                this.txtUserid.Enabled = this.txtPassword.Enabled = true;
            }
        }

        #endregion
    }
}